using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum PieceType
{
    EMPTY,
    NORMAL,
    COUNT,
}
public class GridManager : MonoBehaviour
{
    
    public int xDim;
    public int yDim;
    public float xSpaceScale=1;
    public float ySpaceScale =1;
    public float fillTime;
    public Transform gridHolder;
    
    private Dictionary<PieceType, GameObject> piecePrefabDict;
    [System.Serializable]
    public struct PiecePrefab
    {
        public PieceType type;
        public GameObject prefab;
    }
    public PiecePrefab[] piecePrefabs;
    public GameObject backgroundPrefab;
    private GamePiece [,] pieces;
    void Awake()
    {
        piecePrefabDict = new Dictionary<PieceType, GameObject>();
        for (int i = 0; i < piecePrefabs.Length; i++)
        {
            if (!piecePrefabDict.ContainsKey(piecePrefabs[i].type))
            {
                piecePrefabDict.Add(piecePrefabs[i].type, piecePrefabs[i].prefab);
            }
        }
        #region 背景
        //for (int x = 0; x < xDim; x++)
        //{
        //    for(int y = 0; y < yDim; y++)
        //    {
        //        GameObject background = (GameObject)Instantiate(backgroundPrefab,  gridHolder);
        //        background.transform.localPosition = GetWorldPosition(x*xSpaceScale, y*ySpaceScale);
        //    }
        //}
        #endregion
        pieces = new GamePiece[xDim, yDim];
        for (int x = 0; x < xDim; x++)
        {
            for (int y = 0; y < yDim; y++)
            {
                SpawnNewPiece(x, y, PieceType.EMPTY);
                //GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[PieceType.NORMAL]);
                //newPiece.name = "fruit(" + x + "," + y+")";
                //newPiece.transform.parent = gridHolder;
                //newPiece.transform.localPosition = GetWorldPosition(x * xSpaceScale, y * ySpaceScale);
                //pieces[x, y] = newPiece.GetComponent<GamePiece>();
                //pieces[x, y].Init(x, y, this,PieceType.NORMAL);
                //if (pieces[x, y].IsMovable())
                //{
                //    pieces[x, y].MovableComponent.Move(x, y);
                //}
                //if (pieces[x, y].IsColored())
                //{
                //    pieces[x, y].ColorComponent.SetColor((ColorPiece.ColorType)Random.Range(0,4));
                //}
            }
        }
        StartCoroutine(Fill());
    }

    // Update is called once per frame
    void Update()
    {
        
    }
   public Vector2 GetWorldPosition(float x,float y)
    {
        return new Vector2(gridHolder.position.x - xDim / 2.0f + x, gridHolder.position.y - yDim / 2.0f + y);
    }
    /// <summary>
    /// 填充所有网格
    /// </summary>
    public IEnumerator Fill()
    {
        while (FillStep())
        {
            yield return new WaitForSeconds(fillTime);
        }
    }
    public bool FillStep()
    {
        bool movedPiece = false;
        for (int y = yDim - 2; y >= 0; y--)
        {
            for(int x = 0; x < xDim; x++)
            {
                GamePiece piece = pieces[x, y];
                if (piece.IsMovable())
                {
                  
                    GamePiece pieceBelow = pieces[x, y + 1];
                    if (pieceBelow.Type == PieceType.EMPTY)
                    {
                        Destroy(pieceBelow.gameObject);
                        piece.MovableComponent.Move(x, y + 1,fillTime);
                        pieces[x, y + 1] = piece;
                        SpawnNewPiece(x, y, PieceType.EMPTY);
                        movedPiece = true;
                    }
                }
            }
        }
        for(int x = 0; x < xDim; x++)
        {
            GamePiece pieceBelow = pieces[x, 0];
            if (pieceBelow.Type == PieceType.EMPTY)
            {
                Destroy(pieceBelow.gameObject);
                GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[PieceType.NORMAL],GetWorldPosition(x,-1),Quaternion.identity);
                newPiece.transform.parent = gridHolder;
                pieces[x, 0] = newPiece.GetComponent<GamePiece>();
                pieces[x, 0].Init(x, -1, this, PieceType.NORMAL);
                pieces[x, 0].MovableComponent.Move(x, 0,fillTime);
                pieces[x, 0].ColorComponent.SetColor((ColorPiece.ColorType)Random.Range(0,pieces[x,0].ColorComponent.NumColors));
                movedPiece = true;
            }
        }
        return movedPiece;
    }
    public GamePiece SpawnNewPiece(int x,int y,PieceType type)
    {
        GameObject newPiece = (GameObject)Instantiate(piecePrefabDict[type],GetWorldPosition(x,y), Quaternion.identity,gridHolder);
        newPiece.transform.parent = gridHolder;
        pieces[x, y] = newPiece.GetComponent<GamePiece>();
        pieces[x, y].Init(x, y, this, type);
        return pieces[x, y];
    }
}
